#!/bin/sh
 
###############################################################################
#  Authored by: Demetrios Dimatos
#  Date:        August 23 2006
#  Contact:  	dimatos@gmail.com
###############################################################################

###############################################################################
#  This script will install the Generic Database Explorer.
#  Currently the script is aimed towards Debian bases stystems, of which I am
#  using Ubuntu 6.06. This script is interactive and will prompt for password
#  privlages to install any missing applications.
#
#  Procedures in script
#   - Check OS installation, only debian base supported now.
#   - Obtain root password for continued sudo operations.
#   - check/install/start apache2
#   - check/install/start mysql-server-5.0
#   - change password for mysql-server root 
#   - check/install/start php5
#   - check/install/start mysql for apache http server
#   - check/install/start subversion
#   - wget the latest gde package
#   - Load sample database into mysql from new sandbox
#   - create symbolic link for apache to point to bdap sandbox
#   - Edit file for bdap setup and move it the correct directory
#   - login to bdap create test account
#  
#  Assumptions:
#  - File download will be "gde-*.0.tar.gz" where "*" will be the versioning used.
#  - Will extract to "gde-*.0/" where a "README.txt", "gde_install.sh",
#    and "gde_dev/" will be included
###############################################################################

#  Detect user
USER=`whoami`

#  Path GDE will be home too
PATH_GDE=`pwd`

#  Init path
PATH_INIT=/etc/init.d

#  WWW path
PATH_WWW=/var/www/

#  Sudo passwd
PASSWD_SUDO=""

#  Server Versions
SERVER_VERSION_APACHE=2
SERVER_VERSION_MYSQL="5.0"  #Nothing less than myslq-server4.1 supported
SERVER_VERSION_PHP=5

#  Package manager, this is started for later distros to be added.
PACKAGE_MANAGER=""

# Database info
DB_USERNAME=gdeuser				
DB_PASSWORD=gdeuser
DB_HOST=localhost
DB_DATABASE=BDAP
ERRORS=0

###############################################################################
# FUNCTION: operating_system()
#           - Detects operating system, currently (8/2006) set only to install
#           to debian. If anything other then a debian distro is detected the 
#           script will exit, otherwise it will set the detected info for 
#           printing and the package manager depending on the distro.
###############################################################################
operating_system()
{
    #  OS Detection
    OS=`uname -s`
    KERNEL=`uname -r`
    MACH=`uname -m`
    
    STRING_EXIT="The detected operating system is not supported, exiting."
    STRING_SUCCESS="Debian is supported and will continue installing."
    
    BOOL_EXIT=1  #Bool true to exit
    
    echo " Detecting the operating system to be installed on. Currently "
    echo " only Debian base is supported now."
    echo
    sleep 2
    
    if [ "${OS}" = "SunOS" ]
       then 
          OS=Solaris
    	  RESULT="${OS} ${KERNEL}"
	  BOOL_EXIT=1	    
	    
    elif [ "${OS}" = "AIX" ]
         then 
	    RESULT="${OS} `oslevel` (`oslevel -r`)"
	    BOOL_EXIT=1	
	 
    elif [ "${OS}" = "Linux" ]
         then 
    	 if [ -f /etc/redhat-release ]
	    then 
	      DIST="RedHat `cat /etc/redhat-release`"
	      BOOL_EXIT=1
		 
    	 elif [ -f /etc/SuSE-release ]
	      then 
	        DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
	        BOOL_EXIT=1
    		   		   
    	 elif [ -f /etc/mandrake-release ]
	      then  
	        DIST="Mandrake `cat /etc/mandrake-release`"
	        BOOL_EXIT=1
    		  		  
    	 elif [ -f /etc/debian_version ]
	      then 
	        DIST="Debian `cat /etc/debian_version`"
		PACKAGE_MANAGER="apt-get -y"
	        BOOL_EXIT=0

    	 elif [ -f /etc/UnitedLinux-release ]
	      then 
	        DIST=`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`
	        BOOL_EXIT=1
    	fi
    	
    	RESULT="${OS} ${DIST} ${KERNEL} ${MACH}"
    
    fi
    
    if [ $BOOL_EXIT = 1 ]  # OS is not debian
       then
       echo ${RESULT}
       echo "$STRING_EXIT"
       exit
    else                   # OS is debian continue installation
       echo ${RESULT}
       echo
       echo "$STRING_SUCCESS"
       echo
       sleep 2
    fi
    
}
###############################################################################
# FUNCTION: pause()
#             - pauses shell
###############################################################################
pause()				
{
    echo
    echo -n Hit Enter to continue....
    read
}

###############################################################################
#  FUNCTION: prompt_sudo_passwd()
#              - prompt user for sudo passwd
###############################################################################
prompt_sudo_passwd()
{
  echo "$USER please enter the administrative password to be used for " 
  echo "installing GDE."
  read PASSWD_SUDO
  echo
  #echo "pass is $PASSWD_SUDO"
}

###############################################################################
#  FUNCTION: install_apache()
#            - installs apache, version and package manager is set in globals
###############################################################################
install_apache()
{
  echo "Cecking for apache$SERVER_VERSION_APACHE installation"
  CURR_SERVER_VERSION_APACHE=`ls /etc/init.d/apache$SERVER_VERSION_APACHE |cut -c 13-19`
  
  if [ -z "$CURR_SERVER_VERSION_APACHE"  ]      # Nothing is installed
     then
       echo "Found no installation of apache$SERVER_VERSION_APACHE, preparing to install."
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install apache$SERVER_VERSION_APACHE
       echo $PASSWD_SUDO | sudo -S $PATH_INIT/apache$SERVER_VERSION_APACHE start
       echo
       firefox http://localhost/apache2-default/ &
       echo "You should see a default Apache 2 webpage."
       pause  
       clear
       echo
     else
       echo "Detected previously installed apache$CURR_SERVER_VERSION_APACHE"
       echo "Continuing install of GDE, no need to install apache";
       echo
  fi
}

###############################################################################
#  FUNCTION: install_mysql()
#            - install apache, version and package manager is set in globals 
#            checks to see if its installed, if nothing installed then installs,
#            starts and sets mysql root passwd to the system password, otherwise
#            checks to see if and older version than 4.1 is installed and if so
#            removes older version, installs newer 5.0, starts and sets passwd.
###############################################################################
install_mysql()
{
  echo "Checking for mysql$SERVER_VERSION_MYSQL installation"
  
  # Detect the current version of mysql-server if installed
  if [ -d /etc/mysql ]
     then
       CURR_SERVER_VERSION_MYSQL=`mysql -V 2>&1| awk '{print $5}' |cut -c 1-6`
  fi
  
  if [ -z "$CURR_SERVER_VERSION_MYSQL" ]      # Nothing is installed
     then
       echo "Found no installation of mysql$SERVER_VERSION_MYSQL, preparing to install."
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install mysql-server-$SERVER_VERSION_MYSQL
       echo $PASSWD_SUDO | sudo -S $PATH_INIT/mysql-server-$SERVER_VERSION_MYSQL start
       echo "Root password for MySQL will be set to system Root password entered"
       sleep 3
       mysqladmin -u root password $PASSWD_SUDO
       clear
       
  elif [ 4.1 \> $CURR_SERVER_VERSION_MYSQL ]  # Older version detected < 4.1
     then
       echo "I have detetected an older version of MySQL4.1, currenlty support "
       echo "is for 4.1 and higher. I will attempt to remove the current "
       echo "installation and install version 5."
       pause
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER remove mysql-server-$CURR_SERVER_VERSION_MYSQL
       sleep 3
       echo "Preparing to install mysql-server-$SERVER_VERSION_MYSQL"
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install mysql-server-$SERVER_VERSION_MYSQL
       echo $PASSWD_SUDO | sudo -S $PATH_INIT/mysql-server-$SERVER_VERSION_MYSQL start 
       echo "Root password for MySQL will be set to system Root password entered"
       echo "Please note this, you will be prompted for it later during the installation"
       sleep 3
       mysqladmin -u root password $PASSWD_SUDO
       clear
  else
       echo "Detected previously installed mysql$SERVER_VERSION_MYSQL"
       echo "Continuing intall of GDE, no need to install mysql";
       echo
  fi
}

###############################################################################
#  FUNCTION: install_mysql_php_module()
#            - installs MySQL module for php5, 
###############################################################################
install_mysql_php_module()
{
   echo "Installing php$SERVER_VERSION_PHP-mysql modlue"
   echo
   echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install php$SERVER_VERSION_PHP-mysql
   echo $PASSWD_SUDO | sudo -S $PATH_INIT/apache$SERVER_VERSION_APACHE start
}

###############################################################################
#  FUNCTION: install_php()
#            - installs php5 then it creates a testphp.php file revealing 
#            php info, moves it to /var/www, lauches browser to veiw and 
#            removes the testphp file.
###############################################################################
install_php()
{
  if [ ! -d /etc/php5 ]  #PHP 5 is not installed since no /etc/php5
     then
       echo "Found no installation of php$SERVER_VERSION_PHP, preparing to install."
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install  php$SERVER_VERSION_PHP
       echo "<?php phpinfo(); ?>">testphp_gde.php
       echo $PASSWD_SUDO | sudo -S mv testphp_gde.php $PATH_WWW
       firefox http://localhost/testphp_gde.php &
       echo "You should be seeing a default PHP 5 info webpage."
       pause  
       echo $PASSWD_SUDO | sudo -S rm testphp_gde.php $PATH_WWW
       clear
       
       install_mysql_php_module
       clear
  else
       echo "Detected previously installed php$SERVER_VERSION_PHP"
       echo "Continuing installing GDE, no need to install php";
       echo
  fi	
}

###############################################################################
#  FUNCTION: install_subversion()
#            - installs subversion, intention is that you need subversion to 
#            participate in development of GDE
#            Using svn:
#            Make a sandbox: mkdir gde_dev
#            CD to sandbox:  cd gde_dev
#            Check out GDE:  svn co svn://cvs.cosi.clarkson.edu/bgp-data
#            Enter User and Password for access to Subversion
###############################################################################
install_subversion()
{
  if [ ! -d /etc/subversion ]  #Subversion is not installed since no /etc/subversion
     then
       echo "Found no installation of subversion, preparing to install."
       echo $PASSWD_SUDO | sudo -S $PACKAGE_MANAGER install subversion
       sudo apt-get install subversion
       echo
       clear
   else
       echo "Detected previously installed subversion"
       echo "Continuing installing GDE, no need to install subversion";
       echo
  fi
}

###############################################################################
#  FUNCTION: wget_gde()
#            - obtains latest release of gde and untars it.
###############################################################################
wget_gde()
{
   PATH_GDE=`pwd`  # Where gde file will be
   echo "Downloading the newewst release of GDE"
   wget -r -t 5 -l1 --no-parent -A.gz http://generic-database-explorer.googlecode.com/svn/trunk/
   
   echo "Unpacking the newest release of GDE"
   tar -zxvf gde-*.0.tar.gz   
   echo
}

###############################################################################
#  FUNCTION: load_sample_data()
#            - load the sample data into myaql to test GDE and GDE manager
###############################################################################
load_sample_data()
{
   echo "Loading the GDE DBDAP database, please eneter your password when promted"
   cd samples/databases/
   mysql -u root -p$PASSWD_SUDO < BDAP.sql
   echo
   
   sleep 3
   echo "Granting user $DB_USERNAME full access to *@$DB_HOST with password $DB_PASSWORD"
   mysql -u root -p$PASSWD_SUDO --execute="grant all on *.* to $DB_USERNAME@$DB_HOST identified by '$DB_PASSWORD'"
   echo
   
   sleep 3
   echo "Loading the sample database menagerie, please eneter your password when promted"
   #cd samples/databases/
   mysql -u root -p$PASSWD_SUDO < menagerie.sql
   echo
   
   # sleep 3
   # echo "Granting user $DB_USERNAME full access to menagerie@$DB_HOST with password $DB_PASSWORD"
   # mysql -u root -p$PASSWD_SUDO --execute="grant all on menagerie.* to $DB_USERNAME@$DB_HOST identified by '$DB_PASSWORD'"
   # echo
}

###############################################################################
#  FUNCTION: symlink_gde_apache()
#            - Create the symbolic link for apache to access gde(s) index page
###############################################################################
symlink_gde_apache()
{
   echo "Creating a symbolic link for apache to access gde(s) index page"
   echo $PASSWD_SUDO | sudo -S ln -s $PATH_GDE/gde-*.0/gde_dev/ $PATH_WWW/gde
   echo
}

###############################################################################
#  FUNCTION: copy_gde_apache()
#            - Copy gde to apache to access gde(s) index page
###############################################################################
copy_gde_apache()
{
   echo "Copying gde to /var/www/ "
   cd ../../../
   echo $PASSWD_SUDO | sudo -S mkdir /var/www/gde
   echo $PASSWD_SUDO | sudo -S cp -rf gde-1.0/* $PATH_WWW/gde
   echo
}

###############################################################################
#  FUNCTION: gde_setup()
#            - Edit the file for gde backend setup
###############################################################################
gde_setup()
{
   echo "Setting up the config file for the backend database"
   
   # Read in users info, leave default ones out of users options
   echo "These values can be altered in the config.php located at "
   echo "Database username [default $DB_USERNAME ] "
     #read DB_USERNAME
   echo "Database password [default $DB_PASSWORD ] "
     #read DB_PASSWORD
   echo "Database host [default $DB_HOST ] "
     #read DB_HOST
   echo "Database name [default $DB_DATABASE] "
     #read DB_DATABASE
   
   # Echo out the configuration file based on user options above
   #echo "Writing the config.php file to $PATH_GDE/gde_dev/database/includes/config.php"
   #echo "<?php">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "class Config">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "{">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "">>$PATH_GDE/gde_dev/database/includes/config.php
   #
   #echo "const db_username      =   '$DB_USERNAME';">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "const db_password      =   '$DB_PASSWORD';">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "const db_host          =   '$DB_HOST';">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "const db_database      =   '$DB_DATABASE';">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "const errors           =  $ERRORS;">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "const templatefile     =   '$TEMPLATE_FILE';">>$PATH_GDE/gde_dev/database/includes/config.php
   #            
   #echo "">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "}">>$PATH_GDE/gde_dev/database/includes/config.php
   #echo "?>">>$PATH_GDE/gde_dev/database/includes/config.php

   #  Grant user full access to bdap database with a password
   # sleep 3
   # echo "Granting user $DB_USERNAME full access to $DB_DATABASE@$DB_HOST with password $DB_PASSWORD"
   # mysql -u root -p$DB_PASSWORD --execute="grant all on $DB_DATABASE.* to $DB_USERNAME@$DB_HOST identified by '$DB_PASSWORD'"
}

###############################################################################
#  FUNCTION: gde_launch()
#            - Execute gde so user can create an account
###############################################################################
gde_launch()
{
   echo "Launcing GDE Login page. Please register to gain access"
   echo "Creating an account can be any username and pass combination"
   firefox http://localhost/gde &	
}
###############################################################################
#  MAIN
###############################################################################

#  Detect the OS , look to see if its Debian flavor
operating_system

#  Just pause to let let user digest info
pause

#  Prompt user for the sudo password, will need to manage installation.
prompt_sudo_passwd

#  Install Apache2
install_apache

#  Install mysql
install_mysql

#  Install PHP5
install_php

#  Install Subversion - only enable if you plan on using subversion
#  Maybe question the user on this with a [Y/N]
#install_subversion

#  Download the newest version before installing
#  Maybe question the user on this with a [Y/N]
#  wget_gde

#  Edit file for gde setup
gde_setup

#  Load sample data to veiw in GDE
load_sample_data

#  Create the symbolic link for apache to access gde (NOT using see copy_gde_apache)
#  symlink_gde_apache

# Copy gde to /var/www
copy_gde_apache

#  Launch GDE application
gde_launch
